package com.example.tests;

import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

import com.example.fw.AccountHelper;
import com.example.fw.JamesHelper;
import com.example.fw.User;

public class SignUpTest extends TestBase{
	
	public User user = new User().setLogin("testuser1").setPassword("123456")
			.setEmail("testuser1@localhost.localdomain");
	private JamesHelper james;
	private AccountHelper accHelper;
	
	
	@BeforeClass
	public void createMailUser(){
		james = app.getJamesHelper();
		accHelper = app.getAccountHelper();
		if(!james.doesUserExist(user.login)){	
		james.createUser(user.login, user.password);
		}		
	} 
	
	
	@Test
	public void newUserShouldSignUp() {
	
		accHelper.signup(user);
		accHelper.login(user);
		assertThat(accHelper.loggedUser(), equalTo(user.login));
	}
	
//	@Test
	public void existingUserShouldNotSignUp() {
		try{
		accHelper.signup(user);
		} catch (Exception e){
			assertThat(e.getMessage(), containsString("That username is already being used"));
			return;
		}

		Assert.fail("Exception Expected");
}
	@AfterClass
	public void deleteMailUser(){
		if(james.doesUserExist(user.login)){
			james.deleteUser(user.login);
		}
	}
}
